﻿using web_api_demo101.Entities;

namespace web_api_demo101.Repositories.Impl;

public class UserRepository
{
    public int Add(User user)
    { 
        DbContext.Users.Add(user);
        return 1;
    }

    public int Update(User user) 
    {
        var target = DbContext.Users.Single(x => x.Id == user.Id);

        target.Password = user.Password;
        target.UserName = user.UserName;
        target.Status = user.Status;
        target.LastLoginTime = user.LastLoginTime;

        return 1;
    }

    public User? GetById(int id)
    {
        return DbContext.Users.SingleOrDefault(x => x.Id == id);
    }

    public User? GetByEmail(string email) 
    {
        return DbContext.Users.SingleOrDefault(x => x.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
    }

    public List<User> List()
    {
        return DbContext.Users;
    }
}
